import 'dart:io';
import 'package:extended_image/extended_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

class CacheImage extends StatefulWidget {
  final String image;
  final BoxFit fit;
  final bool cache;
  final String placeholder;
  final double? width;
  final double? height;
  final Alignment alignment;

  const CacheImage({
    Key? key,
    required this.image,
    this.alignment = Alignment.center,
    this.placeholder = 'assets/home/home_person.png',
    this.width,
    this.height,
    this.fit = BoxFit.fill,
    this.cache = true,
  }) : super(key: key);

  @override
  _CacheImageState createState() {
    // TODO: implement createState
    return _CacheImageState();
  }
}

class _CacheImageState extends State<CacheImage> {
  bool isExit = false;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('img === ${widget.image}');
    if (widget.image != null) {
      File file = File(widget.image!);
      if (file.existsSync()) {
        isExit = true;
      }
    }
  }

  @override
  void didUpdateWidget(CacheImage oldWidget) {
    super.didUpdateWidget(oldWidget);
    if (oldWidget.image != widget.image) {
      if (widget.image != null) {
        File file = File(widget.image);
        if (file.existsSync()) {
          isExit = true;
        }
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return widget.image == null || widget.image.isEmpty
        ?
    Image.asset(
      widget.placeholder,
      fit: widget.fit,
      width: widget.width,
      height: widget.height,
    )
        : widget.image!.startsWith('http')
        ? ExtendedImage.network(
      widget.image,
      fit: widget.fit,
      cache: widget.cache,
      width: widget.width,
      height: widget.height,
      alignment: widget.alignment,
      loadStateChanged: (ExtendedImageState state) {
        switch (state.extendedImageLoadState) {
          case LoadState.loading:
            return Image.asset(
              widget.placeholder,
              fit: BoxFit.fill,
            );
            break;
          case LoadState.completed:
            return state.completedWidget;
            break;
          case LoadState.failed:
            return Image.asset(
              widget.placeholder,
              fit: BoxFit.fill,
            );
            break;
        }
        return null;
      },
    )
        : widget.image.startsWith('assets')
        ? Image.asset(
      widget.image,
      fit: widget.fit,
      width: widget.width,
      height: widget.height,
    )
        : isExit
        ? ExtendedImage.file(
      File(widget.image),
      fit: widget.fit,
      width: widget.width,
      height: widget.height,
      alignment: widget.alignment,
      loadStateChanged: (ExtendedImageState state) {
        switch (state.extendedImageLoadState) {
          case LoadState.loading:
            return Image.asset(
              widget.placeholder,
              fit: BoxFit.fill,
            );
            break;
          case LoadState.completed:
            return state.completedWidget;
            break;
          case LoadState.failed:
            return Image.asset(
              widget.placeholder,
              fit: BoxFit.fill,
            );
            break;
        }
        return null;
      },
    )
        : Image.asset(
      widget.placeholder,
      fit: widget.fit,
      width: widget.width,
      height: widget.height,
    );
  }
}
